package com.woniu.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniu.utils.BaseController;
import com.woniu.entity.Goods;
import com.woniu.service.GoodsService;
import com.woniu.utils.MyPage;
import com.woniu.vo.GoodsVo;
import com.woniu.vo.QueryVo;

@Controller
@RequestMapping("/auction/Goods")
public class GoodsController extends BaseController {
	@Autowired
	private GoodsService goodsService;
	
	// 获取商品列表
		@RequestMapping("/productdata")
		@ResponseBody
		public Map<String, Object> getProductData(HttpServletResponse resp, int pageNum, int pageSize, String pname,
				String sellername, String productstatus, String starttime, String endingtime) throws IOException {
			resp.setCharacterEncoding("utf-8");
			// 接收参数：第几页，每页有几条
			System.out.println("接收到的pageNum：" + pageNum);
			System.out.println("接收到的pageSize：" + pageSize);
			System.out.println("接收到的pname：" + pname);
			System.out.println("接收到的sellername：" + sellername);
			System.out.println("接收到的productstatus：" + productstatus);
			System.out.println("接收到的starttime：" + starttime);
			System.out.println("接收到的endingtime：" + endingtime);

			int startIndex = (pageNum - 1) * pageSize;
			System.out.println("从第几个参数开始：" + startIndex);
			// 查询商品数据记录总数

			int count = goodsService.getDataCount(pname, sellername, productstatus, starttime, endingtime);
			// 获取当页数据的查询结果
			List<Map> list = goodsService.getProductList(pageSize, startIndex, pname, sellername, productstatus, starttime,
					endingtime);

			// 查询列表
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("code", 0);
			resultMap.put("msg", "");
			resultMap.put("count", count);
			resultMap.put("data", list);
			// System.out.println("json:" + JSON.toJSONString(resultMap));
			// resp.getWriter().print(JSON.toJSONString(resultMap));
			return resultMap;
		}

		@RequestMapping("/switchstatus")
		@ResponseBody
		public int switchStatus(String productId, String toStatus) {
			System.out.println(productId);
			int result = 0;
			int resultAfterMethod = 0;
			if (toStatus.equals("off")) {
				resultAfterMethod = goodsService.updateStatusOff(productId);
				if (resultAfterMethod == 1) {
					result = resultAfterMethod;
					return result;
				}
			} else if (toStatus.equals("on")) {
				resultAfterMethod = goodsService.updateStatusOn(productId);
				if (resultAfterMethod == 1) {
					result = resultAfterMethod;
					return result;
				}
			} else {
				return result;
			}
			return result;
		}

		@RequestMapping("/mulswitchstatus")
		@ResponseBody
		public int mulSwitchStatus(String[] list, String toStatus) {
			int count = 0;
			int successRecord = 0;

			if (toStatus.equals("off")) {
				// 遍历，发到后台操作
				for (String s : list) {
					// 要修改的记录数量
					count++;
					System.out.println("count:" + count);
					// 实际完成修改的记录数量
					successRecord += goodsService.updateStatusOff(s);
					System.out.println("sucessRecord:" + successRecord);
				}
			} else if (toStatus.equals("on")) {
				// 遍历，发到后台操作
				for (String s : list) {
					// 要修改的记录数量
					count++;
					System.out.println("count:" + count);
					// 实际完成修改的记录数量
					successRecord += goodsService.updateStatusOn(s);
					System.out.println("sucessRecord:" + successRecord);
				}
			} else {
				return 0;
			}
			// 返回
			if (count == successRecord) {
				return count;
			} else {
				return 0;
			}
		}
		
		
		// --分页查询
			@RequestMapping("/selectByConds")
			@ResponseBody
			public MyPage<QueryVo> selectByConds(GoodsVo gv) { 
				MyPage<QueryVo> page = goodsService.selectByConds(gv);
				return page;
			}
			
			@RequestMapping("/selectById")
			@ResponseBody
			public QueryVo selectById(Integer gid,HttpSession session) {
				QueryVo vo = goodsService.selectGoodsById(gid);
				return vo;
			}
			
			@RequestMapping("/updateGoods")
			public String  updateGoodsById(Integer gid,Date uploadtime,String goodstatus) {
				goodsService.updateGoodsById(gid,uploadtime,goodstatus);
				return "paipai/adminiframe/goods";
			}
			
			@RequestMapping("/deleteById")
			@ResponseBody
			public String deleteById(int gid) {
				goodsService.deleteById(gid);
				return "success";
			}

}